/**
 * Name        : PhoneGap
 * Version     : 
 * Vendor      : 
 * Description : 
 */

#include "PhoneGap.h"
#include "PhoneGapForm.h"
#include "ChildPhoneGapForm.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::System;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

PhoneGap::PhoneGap()
{
	__pFormManager = NULL;
}

PhoneGap::~PhoneGap()
{
	if(NULL != __pFormManager)
		delete __pFormManager;
}

Application*
PhoneGap::CreateInstance(void)
{
	// Create the instance through the constructor.
	return new PhoneGap();
}

bool
PhoneGap::OnAppInitializing(AppRegistry& appRegistry)
{
	Frame *pFrame = null;
	result r = E_SUCCESS;

	// Create a form
	PhoneGapForm *pPhoneGapForm = new PhoneGapForm();
	if(NULL == pPhoneGapForm) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r) r = pPhoneGapForm->Construct(FORM_STYLE_NORMAL);

	/* Main Form */
	pFrame = GetAppFrame()->GetFrame();
	pFrame->AddControl(*pPhoneGapForm);
	pFrame->SetCurrentForm(*pPhoneGapForm);
	pPhoneGapForm->Draw();
	pPhoneGapForm->Show();

	/* add key event listener */
	if(E_SUCCESS == r) Osp::Ui::KeyEventManager::GetInstance()->AddKeyEventListener(*this);

	/* FormManager */
	if(NULL == __pFormManager) __pFormManager = new FormManager();
	if(NULL == __pFormManager) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r)
	{
		__pFormManager->SetStartForm(pPhoneGapForm);
		((PhoneGapForm*)__pFormManager->getCurrentForm())->LoadUrl(INIT_PAGE_URL);
	}

	if(E_SUCCESS == r)
		return true;
	else
		return false;
}

bool
PhoneGap::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
{
	return true;
}

void
PhoneGap::OnForeground(void)
{
}

void
PhoneGap::OnBackground(void)
{
}

void
PhoneGap::OnLowMemory(void)
{
}

void
PhoneGap::OnBatteryLevelChanged(BatteryLevel batteryLevel)
{
}

void
PhoneGap::OnScreenOn (void)
{
}

void
PhoneGap::OnScreenOff (void)
{
}

void
PhoneGap::OnUserEventReceivedN (RequestId requestId, Osp::Base::Collection::IList *pArgs)
{
	Osp::Ui::Controls::Form* pForm = NULL;
	Osp::Base::String * pUrl = NULL;
	switch(requestId)
	{
	case EVENT_PRESSED_SIDE_UP_BUTTON:
		break;

	case EVENT_PRESSED_SIDE_DOWN_BUTTON:
		break;

	case EVENT_PRESSED_CAMERA_BUTTON:
		((PhoneGapForm*)__pFormManager->getCurrentForm())->evaluateJavascript(String() + L"document.location='" + INIT_PAGE_URL + L"';");
		break;

	case EVENT_PRESSED_CAMERA_HALF_SHUTTER_BUTTON:
		break;

	case EVENT_CHILD_PHONEGAP_OPEN:
		pForm = (Osp::Ui::Controls::Form*)pArgs->GetAt(0);
		pUrl = (String*)pArgs->GetAt(1);
		pArgs->RemoveAll(false);

		__pFormManager->PushForm(pForm);
		pForm = NULL;

		((PhoneGapForm*)__pFormManager->getCurrentForm())->LoadUrl(*pUrl);
		delete pUrl;
		pUrl = NULL;

		break;
	case EVENT_CHILD_PHONEGAP_CLOSE:
		__pFormManager->PopForm();
		break;

	case EVENT_CHILD_PHONEGAP_PRESSED_DONE_BUTTON:
		__pFormManager->PopForm();
		break;

	default:
		break;
	}

	if(NULL != pArgs)
	{
		pArgs->RemoveAll(true);
		delete pArgs;
	}
}

void
PhoneGap::OnKeyPressed (const Osp::Ui::Control &source, Osp::Ui::KeyCode keyCode)
{
	AppLog("Key Pressed: %d",keyCode);
	switch(keyCode)
	{
	case Osp::Ui::KEY_SIDE_UP:
		SendUserEvent(EVENT_PRESSED_SIDE_UP_BUTTON,NULL);
		break;
	case Osp::Ui::KEY_SIDE_DOWN:
		SendUserEvent(EVENT_PRESSED_SIDE_DOWN_BUTTON,NULL);
		break;
	case Osp::Ui::KEY_CAMERA:
		SendUserEvent(EVENT_PRESSED_CAMERA_BUTTON,NULL);
		break;
	case Osp::Ui::KEY_CAMERA_HALF_SHUTTER:
		SendUserEvent(EVENT_PRESSED_CAMERA_HALF_SHUTTER_BUTTON,NULL);
		break;
	default:
		break;
	}
}
